<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card border="false" shadow="never">
      <template #header>
        <div class="header">
          <span>共 300 条记录</span>
          <el-button
            @click="drawerHandle('add')"
            icon="el-icon-plus"
            round
            size="small"
            type="primary"
          >添加面经</el-button>
        </div>
      </template>

      <!-- 表格 -->
      <el-table :data="list" border style="width: 100%">
        <!-- <el-table-column label="序号" type="index" width="50">

        </el-table-column>-->
        <el-table-column label="序号" type="index" width="50">
          <template slot-scope="{$index}">{{ obj.pageSize*(obj.current-1 )+$index+1}}</template>
        </el-table-column>
        <el-table-column align="center" label="标题" prop="stem"></el-table-column>
        <el-table-column align="center" label="作者" prop="creator"></el-table-column>
        <el-table-column align="center" label="点赞" prop="likeCount" sortable></el-table-column>
        <el-table-column align="center" label="浏览量" prop="views"></el-table-column>
        <el-table-column align="center" label="更新时间" prop="createdAt"></el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="{row}">
            <!-- <pre>{{ row }}</pre> -->
            <!-- 
            <el-button @click="handleClick(scope.row)" size="small" type="text">查看</el-button>
            <el-button size="small" type="text">编辑</el-button>-->
            <div class="actions">
              <i @click="drawerHandle('preview')" class="el-icon-view"></i>
              <i @click="drawerHandle('edit')" class="el-icon-edit-outline"></i>
              <i @click="del(row.id)" class="el-icon-delete"></i>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        :current-page="obj.current"
        :page-size="100"
        :page-sizes="[20, 30, 40, 50]"
        :total="total"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        layout="total, sizes, prev, pager, next, jumper"
      ></el-pagination>

      <!-- 抽屉 -->
      <el-drawer :title="titleType" :visible.sync="drawer">
        <el-form label-width="80px">
          <el-form-item label="标题">
            <el-input placeholder="输入面经标题"></el-input>
          </el-form-item>
          <el-form-item label="内容">富文本编辑器</el-form-item>
          <el-form-item>
            <el-button type="primary">确认</el-button>
            <el-button>取消</el-button>
          </el-form-item>
        </el-form>
      </el-drawer>
    </el-card>
  </div>
</template>

<script>
import { ArticleApi, ArticleDelApi } from '@/api/article'
export default {
  name: 'article-page',
  data () {
    return {
      tableData: [{
        date: '2016-05-02',
        name: '王小虎',
        province: '上海',
        city: '普陀区',
        address: '上海市普陀区金沙江路 1518 弄',
        zip: 200333
      }, {
        date: '2016-05-04',
        name: '王小虎',
        province: '上海',
        city: '普陀区',
        address: '上海市普陀区金沙江路 1517 弄',
        zip: 200333
      }, {
        date: '2016-05-01',
        name: '王小虎',
        province: '上海',
        city: '普陀区',
        address: '上海市普陀区金沙江路 1519 弄',
        zip: 200333
      }, {
        date: '2016-05-03',
        name: '王小虎',
        province: '上海',
        city: '普陀区',
        address: '上海市普陀区金沙江路 1516 弄',
        zip: 200333
      }],
      obj: {
        current: 1,
        pageSize: 10
      },
      list: [],
      total: 0,
      drawer: false,
      title: ''
    }
  },
  created () {
    this.getData()
  },
  computed: {
    titleType () {
      if (this.title == 'add') {
        return '添加文章'
      } else if (this.title == 'edit') {
        return '编辑文章'
      } else {
        return '查看文章'
      }
    }
  },
  methods: {
    handleClick (row) {
      console.log(row);
    },
    async getData () {
      const res = await ArticleApi(this.obj)
      console.log(res, 44455);
      this.list = res.data.rows// 数据
      this.total = res.data.total//总条数
    },
    /* 分页 */
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`);
      this.obj.pageSize = val
      this.getData()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`);
      this.obj.current = val;
      this.getData()
    },
    del (id) {
      // await ArticleDelApi(id)
      // this.$message.success('删除数据成功')

      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        //b表示点击的是确定按钮
        await ArticleDelApi(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    drawerHandle (type) {
      console.log(type, 99);
      this.title = type
      this.drawer = true
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
